﻿/*
Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Text;
using System.Windows.Forms;

namespace TeachMe.Application.Configuration
{
    public class GridProportions
    {
        public string ParentFormName { get; set; }
        public string Name { get; set; }
        public string Columns { get; set; }

        public static void Lock(Form parentForm, DataGridView grid, MachineSettings machineSettings)
        {
            foreach (GridProportions item in machineSettings.GridsProportions)
            {
                if (item.ParentFormName == parentForm.Name && item.Name == grid.Name)
                {
                    string[] cols = item.Columns.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int idx = 0; idx < grid.Columns.Count; idx++)
                    {
                        if (cols.Length > idx)
                        {
                            grid.Columns[idx].Width = Int32.Parse(cols[idx]);
                        }
                    }
                }
            }

            parentForm.Closing += delegate { SaveProportions(parentForm, grid, machineSettings); };
        }

        private static void SaveProportions(Control parent, DataGridView grid, MachineSettings machineSettings)
        {
            GridProportions proportions = null;

            foreach (GridProportions item in machineSettings.GridsProportions)
            {
                if (item.ParentFormName == parent.Name && item.Name == grid.Name)
                {
                    proportions = item;
                }
            }

            using (machineSettings.Edit())
            {
                if (proportions == null)
                {
                    proportions = new GridProportions();
                    proportions.ParentFormName = parent.Name;
                    proportions.Name = grid.Name;

                    machineSettings.GridsProportions.Add(proportions);
                }

                StringBuilder stringBuilder = new StringBuilder();

                for (int idx = 0; idx < grid.Columns.Count; idx++)
                {
                    if (idx > 0)
                    {
                        stringBuilder.Append(",");
                    }
                    stringBuilder.Append(grid.Columns[idx].Width);
                }

                proportions.Columns = stringBuilder.ToString();
            }
        }
    }
}
